#include "MutaTransport.h"
#include <QPalette>
#include <QPlastiqueStyle>

MutaTransport::MutaTransport(QMainWindow* parent,int x,int y)
    : w(parent),
      x(x),
      y(y),
      rewind("<<",parent),
      stop("[ ]",parent),
      play("|>",parent),
      fastForward(">>",parent),
      lineButton("---",parent),
      eventButton("[==]",parent),
      repeatButton("<<||",parent),
      save("Save",parent),
      load("Load",parent),
      back("<",parent),
      forward(">",parent),
      tempoDisplay(parent),
      beatsDisplay(parent),
      lengthDisplay(parent),
      curveButton("~~~",parent),
      autoTendrilButton("=><=",parent),
      blendButton("|/_",parent),
      strangeAttractorButton(")}{(",parent),
      oscthulhuButton("Connect",parent),
      sendPort(parent)
{
    QPalette pal;

    QLinearGradient gradient(0, 0, 0, 255);
    gradient.setColorAt(0.0, QColor(60,60,60,255));
    gradient.setColorAt(0.1, QColor(50,50,50,255));
    gradient.setColorAt(0.6, QColor(0,0,0));
    gradient.setColorAt(0.975, QColor(40,40,40,255));
    //gradient.setColorAt(1.0, QColor(50,50,50,255));
    gradient.setInterpolationMode(QGradient::ColorInterpolation);

    buttonSizeX = parent->geometry().width()*0.0275;
    buttonSizeY = parent->geometry().height()*0.0325;

    QStyle* style = new QPlastiqueStyle;
    rewind.setStyle(style);
    rewind.setGeometry(x,y,
        buttonSizeX,
        buttonSizeY
    );
    rewind.setCheckable( true );
    connect(&rewind,SIGNAL(clicked()),this,SLOT(rewinding()));
    pal = rewind.palette();
    pal.setColor(QPalette::ButtonText,QColor(200,200,200,255));
    pal.setBrush(QPalette::Button,gradient);
    rewind.setPalette(pal);

    back.setStyle(style);
    back.setPalette(pal);
    back.setGeometry(x+buttonSizeX,y,buttonSizeX,buttonSizeY);

    forward.setGeometry(x+(buttonSizeX*4),y,buttonSizeX,buttonSizeY);
    forward.setStyle(style);
    forward.setPalette(pal);


    fastForward.setGeometry(x+(buttonSizeX*5),y,buttonSizeX,buttonSizeY);
    fastForward.setCheckable( true );
    fastForward.setStyle(style);
    fastForward.setPalette(pal);
    connect(&fastForward,SIGNAL(clicked()),this,SLOT(fastForwarding()));


    //=========================================
    //Button set 2
    //=========================================
    gradient.setColorAt(0.0, QColor(40,70,85,255));
    gradient.setColorAt(0.1, QColor(30,60,75,255));
    gradient.setColorAt(0.6, QColor(5,35,50));
    gradient.setColorAt(0.975, QColor(20,50,65,255));
    pal.setBrush(QPalette::Button,gradient);

    lineButton.setGeometry(x+(buttonSizeX*7),y,buttonSizeX,buttonSizeY);
    lineButton.setCheckable(true);
    lineButton.setChecked(true);
    lineButton.setStyle(style);
    lineButton.setPalette(pal);
    connect(&lineButton,SIGNAL(clicked()),this,SLOT(lineEditing()));

    eventButton.setGeometry(x+(buttonSizeX*8),y,buttonSizeX,buttonSizeY);
    eventButton.setCheckable(true);
    eventButton.setStyle(style);
    eventButton.setPalette(pal);
    connect(&eventButton,SIGNAL(clicked()),this,SLOT(eventEditing()));

    curveButton.setGeometry(x+(buttonSizeX*9),y,buttonSizeX,buttonSizeY);
    curveButton.setCheckable(true);
    curveButton.setChecked(false);
    curveButton.setStyle(style);
    curveButton.setPalette(pal);
    connect(&curveButton,SIGNAL(clicked()),this,SLOT(curveEditing()));

    autoTendrilButton.setGeometry(x+(buttonSizeX*10),y,buttonSizeX,buttonSizeY);
    autoTendrilButton.setCheckable(true);
    autoTendrilButton.setChecked(false);
    autoTendrilButton.setStyle(style);
    autoTendrilButton.setPalette(pal);
    connect(&autoTendrilButton,SIGNAL(clicked()),this,SLOT(curveEditing()));

    //autoTendrilButton.hide();

    blendButton.setGeometry(x+(buttonSizeX*11),y,buttonSizeX,buttonSizeY);
    blendButton.setCheckable(true);
    blendButton.setChecked(false);
    blendButton.setStyle(style);
    blendButton.setPalette(pal);
    connect(&blendButton,SIGNAL(clicked()),this,SLOT(curveEditing()));

    blendButton.hide();

    strangeAttractorButton.setGeometry(x+(buttonSizeX*12),y,buttonSizeX,buttonSizeY);
    strangeAttractorButton.setCheckable(true);
    strangeAttractorButton.setChecked(false);
    strangeAttractorButton.setStyle(style);
    strangeAttractorButton.setPalette(pal);
    connect(&strangeAttractorButton,SIGNAL(clicked()),this,SLOT(strangeAttractorEditing()));

    strangeAttractorButton.hide();

    repeatButton.setGeometry(x+(buttonSizeX*13),y,buttonSizeX,buttonSizeY);
    repeatButton.setCheckable(true);
    repeatButton.setChecked(false);
    repeatButton.setStyle(style);
    repeatButton.setPalette(pal);
    connect(&repeatButton,SIGNAL(clicked()),this,SLOT(repeatEditing()));


    //============================
    //Button set 3
    //============================
    gradient.setColorAt(0.0, QColor(60,60,60,255));
    gradient.setColorAt(0.1, QColor(50,50,50,255));
    gradient.setColorAt(0.6, QColor(0,0,0));
    gradient.setColorAt(0.975, QColor(40,40,40,255));
    pal.setBrush(QPalette::Button,gradient);

    save.setGeometry(x+(buttonSizeX*15),y,buttonSizeX,buttonSizeY);
    save.setToolTip(tr("Save session to a file"));
    save.setStyle(style);
    save.setPalette(pal);

    load.setStyle(style);
    load.setPalette(pal);
    load.setGeometry(x+(buttonSizeX*16),y,buttonSizeX,buttonSizeY);

    tempoDisplay.setStyle(style);
    tempoDisplay.setPalette(pal);
    tempoDisplay.setGeometry(QRect(x+(buttonSizeX*18),y,buttonSizeX*2,buttonSizeY));
    tempoDisplay.setMaximum(300);
    tempoDisplay.setMinimum(1.0);
    tempoDisplay.setValue(120.0);
    tempoDisplay.setFocusPolicy(Qt::StrongFocus);

    beatsDisplay.setStyle(style);
    beatsDisplay.setPalette(pal);
    beatsDisplay.setGeometry(QRect(x+(buttonSizeX*20.5),y,buttonSizeX,buttonSizeY));
    beatsDisplay.setValue(4);
    beatsDisplay.setFocusPolicy(Qt::StrongFocus);
    beatsDisplay.clearFocus();
    beatsDisplay.setMinimum(1);

    lengthDisplay.setStyle(style);
    lengthDisplay.setPalette(pal);
    lengthDisplay.setGeometry(QRect(x+(buttonSizeX*22),y,buttonSizeX,buttonSizeY));
    lengthDisplay.setValue(4);
    lengthDisplay.setFocusPolicy(Qt::StrongFocus);
    lengthDisplay.clearFocus();
    lengthDisplay.setMinimum(1);

    sendPort.setStyle(style);
    sendPort.setPalette(pal);
    //sendPort.setFocusPolicy(Qt::StrongFocus);
    //sendPort.clearFocus();
    sendPort.setMinimum(1);
    sendPort.setMaximum(65535);
    sendPort.setValue(32254);
    sendPort.setGeometry(
        parent->geometry().width()*0.85,
        parent->geometry().height()*0.005,
        parent->geometry().width()*0.065,
        parent->geometry().height()*0.035
    );

    gradient.setColorAt(0.0, QColor(50,100,55,255));
    gradient.setColorAt(0.1, QColor(30,80,35,255));
    gradient.setColorAt(0.6, QColor(10,60,15));
    gradient.setColorAt(0.975, QColor(30,80,35,255));
    pal.setBrush(QPalette::Button,gradient);

    oscthulhuButton.setStyle(style);
    oscthulhuButton.setPalette(pal);
    oscthulhuButton.setGeometry(QRect(x+(buttonSizeX*24),y,buttonSizeX*1.5,buttonSizeY));


    gradient.setColorAt(0.0, QColor(60,60,60,255));
    gradient.setColorAt(0.1, QColor(50,50,50,255));
    gradient.setColorAt(0.6, QColor(0,0,0));
    gradient.setColorAt(0.975, QColor(40,40,40,255));
    gradient.setColorAt(0.6, QColor(0,0,0));
    pal.setBrush(QPalette::Button,gradient);
    stop.setStyle(style);
    stop.setGeometry(x+(buttonSizeX*2),y,buttonSizeX,buttonSizeY);
    pal = stop.palette();
    pal.setBrush(QPalette::Button,gradient);
    pal.setColor(QPalette::ButtonText,QColor(255,0,0,255));
    stop.setPalette(pal);
    stop.setCheckable( true );
    connect(&stop,SIGNAL(clicked()),this,SLOT(stopping()));

    play.setStyle(style);
    play.setGeometry(x+(buttonSizeX*3),y,buttonSizeX,buttonSizeY);
    pal = play.palette();
    pal.setBrush(QPalette::Button,gradient);
    pal.setColor(QPalette::ButtonText, QColor(0, 200, 50));
    play.setPalette(pal);
    play.setCheckable( true );
    connect(&play,SIGNAL(clicked()),this,SLOT(playing()));
}

void MutaTransport::strangeAttractorEditing()
{
    strangeAttractorButton.setChecked(true);
    autoTendrilButton.setChecked(false);
    blendButton.setChecked(false);
    curveButton.setChecked(false);
    lineButton.setChecked(false);
    eventButton.setChecked(false);
    repeatButton.setChecked(false);
}

void MutaTransport::tendrilEditing()
{
    strangeAttractorButton.setChecked(false);
    autoTendrilButton.setChecked(true);
    blendButton.setChecked(false);
    curveButton.setChecked(false);
    lineButton.setChecked(false);
    eventButton.setChecked(false);
    repeatButton.setChecked(false);
}
void MutaTransport::blendEditing()
{
    strangeAttractorButton.setChecked(false);
    autoTendrilButton.setChecked(false);
    blendButton.setChecked(true);
    curveButton.setChecked(false);
    lineButton.setChecked(false);
    eventButton.setChecked(false);
    repeatButton.setChecked(false);
}
void MutaTransport::curveEditing()
{
    strangeAttractorButton.setChecked(false);
    curveButton.setChecked(true);
    lineButton.setChecked(false);
    eventButton.setChecked(false);
    repeatButton.setChecked(false);
    autoTendrilButton.setChecked(false);
    blendButton.setChecked(false);
}
void MutaTransport::lineEditing()
{
    strangeAttractorButton.setChecked(false);
    lineButton.setChecked(true);
    eventButton.setChecked(false);
    repeatButton.setChecked(false);
    curveButton.setChecked(false);
    autoTendrilButton.setChecked(false);
    blendButton.setChecked(false);
}
void MutaTransport::eventEditing()
{
    strangeAttractorButton.setChecked(false);
    lineButton.setChecked(false);
    eventButton.setChecked(true);
    repeatButton.setChecked(false);
    curveButton.setChecked(false);
    autoTendrilButton.setChecked(false);
    blendButton.setChecked(false);
}
void MutaTransport::repeatEditing()
{
    strangeAttractorButton.setChecked(false);
    lineButton.setChecked(false);
    eventButton.setChecked(false);
    repeatButton.setChecked(true);
    curveButton.setChecked(false);
    autoTendrilButton.setChecked(false);
    blendButton.setChecked(false);
}


void MutaTransport::rewinding()
{
    rewind.setChecked(true);
    stop.setChecked(false);
    play.setChecked(false);
    fastForward.setChecked(false);
}
void MutaTransport::stopping()
{
    rewind.setChecked(false);
    stop.setChecked(true);
    play.setChecked(false);
    fastForward.setChecked(false);
}
void MutaTransport::playing()
{
    rewind.setChecked(false);
    stop.setChecked(false);
    play.setChecked(true);
    fastForward.setChecked(false);
}

void MutaTransport::fastForwarding()
{
    rewind.setChecked(false);
    stop.setChecked(false);
    play.setChecked(false);
    fastForward.setChecked(true);
}

QPushButton* MutaTransport::getRewind()
{
    return &rewind;
}
QPushButton* MutaTransport::getStop()
{
    return &stop;
}
QPushButton* MutaTransport::getPlay()
{
    return &play;
}
QPushButton* MutaTransport::getFastForward()
{
    return &fastForward;
}
QPushButton* MutaTransport::getLineButton()
{
    return &lineButton;
}
QPushButton* MutaTransport::getEventButton()
{
    return &eventButton;
}
QPushButton* MutaTransport::getRepeatButton()
{
    return &repeatButton;
}
QPushButton* MutaTransport::getCurveButton()
{
    return &curveButton;
}
QPushButton* MutaTransport::getTendrilButton()
{
    return &autoTendrilButton;
}
QPushButton* MutaTransport::getBlendButton()
{
    return &blendButton;
}
QPushButton* MutaTransport::getStrangeAttractorButton()
{
    return &strangeAttractorButton;
}
QPushButton* MutaTransport::getSave()
{
    return &save;
}
QPushButton* MutaTransport::getLoad()
{
    return &load;
}
QPushButton* MutaTransport::getBack()
{
    return &back;
}
QPushButton* MutaTransport::getForward()
{
    return &forward;
}

QDoubleSpinBox* MutaTransport::getTempoDisplay()
{
    return (QDoubleSpinBox*)&tempoDisplay;
}
QSpinBox* MutaTransport::getBeatsDisplay()
{
    return (QSpinBox*)&beatsDisplay;
}
QSpinBox* MutaTransport::getLengthDisplay()
{
    return (QSpinBox*)&lengthDisplay;
}
QSpinBox* MutaTransport::getSendPort()
{
    return (QSpinBox*)&sendPort;
}

QPushButton* MutaTransport::getOSCthulhuButton()
{
    return &oscthulhuButton;
}

void MutaTransport::windowResized()
{
    buttonSizeX = w->geometry().width()*0.0276;
    buttonSizeY = w->geometry().height()*0.035;
    rewind.setGeometry(x,y,buttonSizeX,buttonSizeY);
    back.setGeometry(x+buttonSizeX,y,buttonSizeX,buttonSizeY);
    forward.setGeometry(x+(buttonSizeX*4),y,buttonSizeX,buttonSizeY);
    fastForward.setGeometry(x+(buttonSizeX*5),y,buttonSizeX,buttonSizeY);

    lineButton.setGeometry(x+(buttonSizeX*7),y,buttonSizeX,buttonSizeY);
    eventButton.setGeometry(x+(buttonSizeX*8),y,buttonSizeX,buttonSizeY);
    curveButton.setGeometry(x+(buttonSizeX*9),y,buttonSizeX,buttonSizeY);
    autoTendrilButton.setGeometry(x+(buttonSizeX*10),y,buttonSizeX,buttonSizeY);
    strangeAttractorButton.setGeometry(x+(buttonSizeX*12),y,buttonSizeX,buttonSizeY);
    repeatButton.setGeometry(x+(buttonSizeX*13),y,buttonSizeX,buttonSizeY);
    blendButton.setGeometry(x+(buttonSizeX*11),y,buttonSizeX,buttonSizeY);

    save.setGeometry(x+(buttonSizeX*15),y,buttonSizeX,buttonSizeY);
    load.setGeometry(x+(buttonSizeX*16),y,buttonSizeX,buttonSizeY);
    tempoDisplay.setGeometry(QRect(x+(buttonSizeX*18),y,buttonSizeX*2,buttonSizeY));
    beatsDisplay.setGeometry(QRect(x+(buttonSizeX*20.5),y,buttonSizeX,buttonSizeY));
    lengthDisplay.setGeometry(QRect(x+(buttonSizeX*22),y,buttonSizeX,buttonSizeY));
    oscthulhuButton.setGeometry(QRect(x+(buttonSizeX*24),y,buttonSizeX*1.5,buttonSizeY));

    stop.setGeometry(x+(buttonSizeX*2),y,buttonSizeX,buttonSizeY);
    play.setGeometry(x+(buttonSizeX*3),y,buttonSizeX,buttonSizeY);
}
